from observers import *
from evolution import *
from evaluate import *
from operators import *

def cga_onemax(chromosomeLen, theta):
	Evolution.evolutionObservers = [stdoutObserver]
	c = Cga(1000, onemax, chromosomeLen, theta)
	c()

def pbil_onemax(populationSize, chromosomeLen, theta1, theta2, theta3):
	Evolution.evolutionObservers = [stdoutObserver]
	p = Pbil(1000, onemax, populationSize, chromosomeLen, theta1, theta2, theta3)
	p()

def ssga_onemax(populationSize, chromosomeLen, blockSize):
	Evolution.evolutionObservers = [stdoutObserver]
	s = Ssga(1000, onemax, populationSize, chromosomeLen, blockSize)
	s()



def testPbil():

    Evolution.evolutionObservers = [observers.stdoutObserver, observers.stderrObserver]

    evaluator = evalAssetsReturn
    evaluator.setStockSymbol('cerabud', 300, 1)
    evaluator.setInitialAsset(100)
    evaluator.setStrategies(strategies.strategies)
    # evaluator.attachObserver(observers.stdoutObserver)

    iterations     = 100
    chromosomeLen  = len(strategies.strategies)
    theta1         = 0.05
    theta2         = 0.005
    theta3         = 0.005
    populationSize = 15

    c = Pbil(evaluator, iterations, populationSize, chromosomeLen, theta1, theta2, theta3)
    c()


def testCga():

    Evolution.evolutionObservers = [observers.stdoutObserver, observers.stderrObserver]

    evaluator = evalAssetsReturn
    evaluator.setStockSymbol('cerabud')
    evaluator.setInitialAsset(100)
    evaluator.setStrategies(strategies.strategies)
    # evaluator.attachObserver(observers.stdoutObserver)

    iterations     = 500
    chromosomeLen = len(strategies.strategies)
    theta         = 0.02
    c = Cga(evaluator, iterations, chromosomeLen, theta)
    c()


def testSsga():

    Evolution.evolutionObservers = [observers.stdoutObserver, observers.stderrObserver]

    evaluator = evalAssetsReturn
    evaluator.setStockSymbol('cerabud')
    evaluator.setInitialAsset(100)
    evaluator.setStrategies(strategies.strategies)
    # evaluator.attachObserver(observers.stdoutObserver)

    iterations     = 100
    chromosomeLen  = len(strategies.strategies)
    #theta          = 0.02
    populationSize = 20
    blockSize      = int(0.30*chromosomeLen)
    thetaM         = 0.05

    selection = BestBlockSelection()
    #selection = RouletteBlockSelection(fitnessFunction)
    #selection = RouletteBlockSelection(linearRank)

    crossover = UniformCrossover()
    #crossover = OnepointCrossover()
    
    mutation = Mutation()

    c = Ssga(evaluator, iterations, selection, crossover, mutation, populationSize, chromosomeLen, blockSize, thetaM)
    c()

if __name__ == "__main__":
    testPbil()
    #testSsga()

